package com.zt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zt.dao.Bannerdao;
import com.zt.dao.Storedao;
import com.zt.service.BannerService;
import com.zt.uitl.Query;
import org.example.systemset_pojo.Banner;
import org.example.systemset_pojo.Store;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BannerServiceImpl extends ServiceImpl<Bannerdao, Banner> implements BannerService {
    @Autowired
    private Bannerdao bannerdao;
    @Autowired
    private Storedao storedao;
    @Override
    public Page<Banner> selectpage(Query query) {
        Page<Banner> page = new Page<>(query.getPage(),query.getSize());
        QueryWrapper<Banner> bannerQueryWrapper = new QueryWrapper<>();
        bannerQueryWrapper.like(query.getName() != null && query.getName().length() > 0,"banner_name",query.getName());
        bannerQueryWrapper.orderByAsc("sort");
        bannerdao.selectPage(page, bannerQueryWrapper);
        List<Banner> records = page.getRecords();
        if (records.size()>0){
            for (Banner b : records) {
                Integer id = b.getStoreId();
                Store store = storedao.selectById(id);
                if (store != null) {
                    b.setStoreName(store.getName());
                } else {
                    b.setStoreName(""); // 设置默认值
                }
            }
        }
        return page;
    }
}
